import { ref, getCurrentInstance } from "vue";
import { dataTypes } from "@/utils/index";
export function useModalEvent({ props }) {
  const { emit } = getCurrentInstance();

  const isLoading = ref(false);
  const isLoadingInner = ref(false);

  const useBeforeChange = () => {
    const isFunction = dataTypes().isFunction(props.beforeChange);
    if (isFunction) {
      isLoading.value = true;
      props
        .beforeChange()
        .then(() => {
          isLoading.value = false;
          emit("ok");
        })
        .catch(() => {
          isLoading.value = false;
        });
      return;
    }
    emit("ok");
  };

  /***
   * 加载内容
   */
  const useInnerLoading = () => {
    const isFunction = dataTypes().isFunction(props.innerLoading);
    if (!isFunction) {
      return;
    }
    // 执行
    isLoadingInner.value = true;
    props
      .innerLoading()
      .then(() => {
        isLoadingInner.value = false;
      })
      .catch(() => {
        isLoadingInner.value = false;
      });
  };

  return {
    isLoading,
    isLoadingInner,
    useBeforeChange,
    useInnerLoading,
  };
}
